<template>
  <div class="contain">
    <div class="flex contain-flex">
      <div class="flex header-left" v-if="$route.meta.role !== 'admin'">
        <div class="nav">
          <div class="nav-item" @click="navTo('/index')" :class="$route.meta.title === 'index' ? 'nav-item-selected' : ''">首页</div>
          <div class="nav-item" @click="navTo('/communication')" :class="$route.meta.title === 'communication' || $route.meta.title === 'communication-detail' || $route.meta.title === 'user-detail' ? 'nav-item-selected' : ''">交流区</div>
          <div class="nav-item" @click="navTo('/post')" :class="$route.meta.title === 'post' ? 'nav-item-selected' : ''">发布帖子</div>
          <div class="nav-item" @click="navTo('/upload')" :class="$route.meta.title === 'upload' ? 'nav-item-selected' : ''">资源上传</div>
          <div class="nav-item" @click="navTo('/info')" :class="$route.meta.title === 'info' ? 'nav-item-selected' : ''">个人管理</div>
        </div>
      </div>
      <div class="flex space-avatar">
        <a-avatar :src="'http://zctuchuang.oss-cn-shenzhen.aliyuncs.com/'+ avatar" class="avatar"/>
        <div class="username">{{ userName }}</div>
        <div class="flex caret-down">
          <a-dropdown :trigger="['click']" placement="bottomRight">
            <a class="ant-dropdown-link" @click="e => e.preventDefault()">
              <a-icon type="caret-down" class="white"/>
            </a>
            <a-menu slot="overlay" class="dropdown-top">
              <template v-if="userId === ''">
                <a-menu-item key="1">
                  <a @click="login">登录</a>
                </a-menu-item>
              </template>
             <template v-else>
               <a-menu-item key="0" v-if="$route.meta.role !== 'admin'">
                 <a @click="changeAvatar(userId)">修改头像</a>
               </a-menu-item>
               <a-menu-item key="1" v-if="$route.meta.role !== 'admin'">
                 <a @click="resetPwd">修改密码</a>
               </a-menu-item>
               <a-menu-item key="2">
                 <a @click="logout(userId)">退出登录</a>
               </a-menu-item>
             </template>
            </a-menu>
          </a-dropdown>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {logoutUser} from "@/api";

  export default {
    name: "IndexHeader.vue",
    data(){
      return{
        count: 5,
        show: true,
        userId: '',
        userName: '',
        avatar: ''
      }
    },
    created() {
      this.userId = window.localStorage.getItem('userId')
      this.userName = window.localStorage.getItem('userName')
      this.avatar = window.localStorage.getItem('avatar')
    },
    methods: {
      //退出登录
      logout() {
        if(this.userId) {
          let params = {
            userId: this.userId,
          }
          // console.log("params+++++++++" + params.userId);
        logoutUser(params)
            .then((res) => {
              console.log('注销登录', res)
              if (res.status == 200){
                window.localStorage.setItem('userId', '')
                window.localStorage.setItem('avatar', '')
                window.localStorage.setItem('userName', '')
                window.localStorage.setItem('token', '')
                this.$message.success('退出登录成功！');
                this.$router.replace('/login')
              }else{
                this.$message.info(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            })
        } else {
          this.$message.info('登出失败！');
        }
        
      },
      //跳转登录页面
      login() {
        this.$router.push('/login')
      },
      //修改密码
      resetPwd() {
        this.$router.push('/change-password')
      },
      //修改头像
      changeAvatar() {
        this.$router.push('/change-avatar')
      },
      navTo(path) {
        this.$router.push(path)
      }
    }
  }
</script>

<style lang="less" scoped>
  .flex{
    display: flex;
    flex-direction: row;
    justify-items: center;
    justify-content: center;
    align-content: center;
    align-items: center;
  }
  .contain{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 68px;
    background-color: #1bcbb4;
    margin-top: 0px;
  }
  .contain-flex{
    justify-content: space-between;
    width: 1280px;
    height: 50px;
    background-color: #1bcbb4;
  }
  .header-left{
    justify-content: space-between;
  }
  .header-right{
    margin-right: 10px;
  }
  .title{
    font-family: MicrosoftYaHei;
    font-size: 16px;
    font-weight: bold;
    font-stretch: normal;
    line-height: 40px;
    letter-spacing: 1px;
    color: #1a1918;
    margin-right: 30px;
    margin-left: 6px;
  }
  .avatar{
    border: 2px solid #ffffff;
    width: 50px;
    height: 50px;
  }
  .search_icon{
    color: #5dd5c8;
    width: 35px;
    height: 35px;
    background-color: #ffffff;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    margin-left: 10px;
    font-size: 11px;
    cursor: pointer;
  }
  .search_icon:hover{
    background-color: #1bcbb4;
    color: #ffffff;
    border: 1px solid #ffffff;
  }
  .space-avatar{
    margin-left: 46px;
  }
  .msg_icon{
    color: #f6f6f6;
    font-size: 16px;
    border-radius: 50%;
  }
  .caret-down{
    border-radius: 50%;
    width: 20px;
    height: 20px;
    border: 2px solid #f6f6f6;
    color: #F6F6F6;
    margin-left: 20px;
    margin-right: 10px;
  }
  .white{
    color: #f6f6f6;
  }
  .dropdown-top{
    margin-top: 10px;
  }
  .nav {
    display: flex;
    color: #fff;
    font-size: 17px;
    height: 40px;
    width: 650px;
  }
  .nav-item {
    padding: 0px 20px;
    box-sizing: border-box;
    border-radius: 10px;
    cursor: pointer;
    margin-right: 20px;
    text-align: center;
    display: flex;
    align-items: center;
  }
  .nav-item-selected {
    background-color: #fff;
    color: #1bcbb4;
  }
  .nav-item:hover {
    background-color: #fff;
    color: #1bcbb4;
  }
  .space-avatar{
    margin-left: 10px;
  }
  .username{
    font-family: MicrosoftYaHei;
    font-size: 14px;
    font-weight: bold;
    font-stretch: normal;
    letter-spacing: 1px;
    color: #f6f6f6;
    margin-left: 10px;
  }

  /*覆盖ant默认input样式*/
  .flex {
    :global(.ant-input){
      padding: 18px;
      width: 300px;
    }
  }
  .header-right{
    :global(.ant-btn){
      background-color: #5dd5c8;
      border: 1px solid #ffffff;
      color: #ffffff;
      padding: 18px 10px;
      vertical-align: middle;
      line-height: 3px;
    }
    :global(.ant-btn:hover){
      background-color: #ffffff;
      color: #1bcbb4;
      border: 1px solid #1bcbb4;
    }
  }
</style>
